package com.yanjun.messenger;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.yanjun.progress.R;


public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        Button btnTest = findViewById(R.id.btnTest);
        btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("test", "activity2 发送的消息");
                for (int i = 0; i < 100; i++) {
                    MagicMessenger.post("MagicActivity", bundle);
                }
            }
        });
        MagicMessenger.subscribe("activity2", new MessageCallback() {
            @Override
            public void onMsgCallBack(Bundle data) {
                String test = data.getString("test");
                String text = "activity2 接收到消息:" + test;
                Toast.makeText(Main2Activity.this, text, Toast.LENGTH_SHORT).show();
                Log.e("activity2", "onMsgCallBack: " + text);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MagicMessenger.unsubscribe("activity2");
    }
}
